import { start } from 'qiankun'
import devSubApps from './devSubApps';

let subApps = []

/**
 * 子应用配置
 * 开发环境: devSubApps
 * 生产环境: window.__SUB_APPS__
 */
switch (import.meta.env.VITE_APP_ENV) {
    case 'development':
        // subApps = devSubApps;
        subApps = window.__SUB_APPS__;  // 本地测试线上环境
        break;
    case 'production':
        subApps = window.__SUB_APPS__;
        break;
    default:
        subApps = devSubApps;
        break;
}

export const qiankunConfig = {
    /* apps: subApps.map((app, index) => ({
        name: app.name,
        entry: app.entry,
        container: `#subapp-container-${app.name}`,
        activeRule: `/${app.name}`,
        props: {
            routerBase: `/${app.name}`,
            onGlobalStateChange: (state, prevState) => {
                console.log('子应用状态变更:', state, prevState);
            }
        }
    })), */
    apps: [
        {
            name: subApps[0].name,
            entry: subApps[0].entry,
            container: '#subapp-container',
            activeRule: subApps[0].activeRule,
            props: {
                routerBase: '/qiankun',
                onGlobalStateChange: (state, prevState) => {
                    console.log('子应用状态变更:', state, prevState)
                }
            }
        },
        {
            name: subApps[1].name,
            entry: subApps[1].entry,
            container: '#subapp-container1',
            activeRule: subApps[1].activeRule,
            props: {
                routerBase: '/qiankun1',
                onGlobalStateChange: (state, prevState) => {
                    console.log('子应用状态变更:', state, prevState)
                }
            }
        }
    ],
    lifeCycles: {
        beforeLoad: app => console.log('before load', app.name),
        afterMount: app => console.log('after mount', app.name)
    }
}

export const startQiankun = () => {
    return start({
        sandbox: {
            experimentalStyleIsolation: true,
            strictStyleIsolation: false,
            // 新增模块联邦配置
            moduleMode: true,
            useNativeWindow: true
        },
        prefetch: 'all'
    })
}
